package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizErrorCode;
import com.atguigu.gulimall.member.ex.PhoneExsitException;
import com.atguigu.gulimall.member.ex.UserNameExsitException;
import com.atguigu.gulimall.member.feign.MemberCoupons;
import com.atguigu.gulimall.member.vo.UserLoginVo;
import com.atguigu.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 会员
 *
 * @author lxx
 * @email 1914264274@qq.com
 * @date 2022-03-08 15:21:40
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    MemberCoupons memberCoupons;

    //测试远程调用
    @RequestMapping("/testFeign")
    public R testMerberCoupos() {
        R resp = R.ok();
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("布丁");
        R coupons = memberCoupons.getCoupons();
        resp.put("member", memberEntity);
        resp.put("coupon", coupons);
        return resp;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @RequestMapping("/regUser")
    public R regUser(@RequestBody UserRegisterVo userRegisterVo) {
        try {
            memberService.regUser(userRegisterVo);
        } catch (UserNameExsitException exception) {
            //用户名重复异常
            return R.error(BizErrorCode.USERNAME_EXCEPTION.getCode(), BizErrorCode.USERNAME_EXCEPTION.getMessage());
        } catch (PhoneExsitException exception) {
            //手机号重复
            return R.error(BizErrorCode.PHONE_EXCEPTION.getCode(), BizErrorCode.PHONE_EXCEPTION.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R dealLogin(@RequestBody UserLoginVo userLoginVo) {
        MemberEntity memberEntity = memberService.login(userLoginVo);
        if (memberEntity == null) {
            return R.error(BizErrorCode.USERACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizErrorCode.USERACCT_PASSWORD_INVALID_EXCEPTION.getMessage());
        } else {
            //登录成功
            R r = new R();
            r.setData(memberEntity);
            return r;
        }

    }

    @PostMapping("/socialLogin")
    public R dealSocialLogin(@RequestBody MemberEntity memberEntity) {
        MemberEntity entity = memberService.socialLogin(memberEntity);
        R r = new R();
        r.setData(entity);
        return r;
    }


}
